//
//  WRCircleLayout.m
//  01-UICollectionView显示图片
//
//  Created by 紫荆秋雪 on 16/3/5.
//  Copyright © 2016年 紫荆秋雪. All rights reserved.
//

#import "WRCircleLayout.h"

@implementation WRCircleLayout
//当内容改变时就重新重新布局
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{

    return YES;
}

//设置 Item的属性
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{

    //1、获取属性
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];

    //2、设置大小
    attrs.size = CGSizeMake(50, 50);
    
    //3、圆的半径
    CGFloat circleRadius = 70;
    CGPoint circleCenter = CGPointMake(self.collectionView.frame.size.width * 0.5, self.collectionView.frame.size.height * 0.5);
    
    //4、设置每个Item之间的角度
    CGFloat angleDelta = M_PI * 2 / [self.collectionView numberOfItemsInSection:indexPath.section];
    
    //5、当前Item的角度
    CGFloat angle = indexPath.item * angleDelta;
    
    attrs.center = CGPointMake(circleCenter.x + circleRadius * cosf(angle), circleCenter.y + circleRadius * sinf(angle));
    
    attrs.zIndex = indexPath.item;
    return attrs;
    
}

//给每一个Item设置布局属性，然后在把布局属性 存储在数组中
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{

    //定义一个可变数组
    NSMutableArray *arrayM = [NSMutableArray array];
    
    //获取数组的个数
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    
    for (int i = 0; i < count; i++) {
        
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        [arrayM addObject:attrs];
    }
    return arrayM;
}

@end
